<?php

error_reporting(E_ALL);

$version_new = '0.9.3';

define('INCLUDE_PATH', 'include/');

include('include/config.inc.php');
include('include/functions.inc.php');
include('include/db.class.php');
include('include/categories.class.php');
include('include/data.class.php');
include('include/forums.class.php');

$db = new sql_db($database['host'], $database['user'], $database['password'], $database['database']);

if (empty($version))
{
	$version = '0.8';
}

if ($version < '0.9')	{ update_0_9(); }
if ($version < '0.9.1')	{ update_0_9_1(); }
if ($version < '0.9.2') { update_0_9_2(); }
if ($version < '0.9.3') { update_0_9_3(); }

update_htaccess(dirname($_SERVER['PHP_SELF']) . '/');
update_conf_file();
update_conf_table();
sync_all_forums();

echo '<p>Update complete. Please remove the files <code>install.php</code> and <code>update.php</code>, then <a href="./">visit your forums</a>!</p>';

function update_0_9()
{	
	echo '<p>Updating to version 0.9...';
	
	global $version, $db;
	
	$sql = "INSERT INTO config VALUES ('dnsbl', '')";
	
	$db->sql_query($sql);

	echo ' done.</p>';

	$version = '0.9';
}

function update_0_9_1()
{
	global $version;
	
	echo '<p>Updating to version 0.9.1... done.</p>';

	$version = '0.9.1';
}

function update_0_9_2()
{
	global $version, $db;

	echo '<p>Updating to version 0.9.2...';
	
	$sql = "ALTER TABLE sessions MODIFY COLUMN page varchar(250) DEFAULT '' NOT NULL";

	$db->sql_query($sql);
	
	echo ' done.</p>';

	$version = '0.9.2';
}

function update_0_9_3()
{
	global $version, $db;

	echo '<p>Updating to version 0.9.3... ';

	$sql = 'CREATE INDEX %1$s ON %2$s (%1$s)';

	$db->sql_query(sprintf($sql, 'forum', 'posts'));
	$db->sql_query(sprintf($sql, 'parent_id', 'posts'));
	$db->sql_query(sprintf($sql, 'is_sticky', 'posts'));
	$db->sql_query(sprintf($sql, 'last_reply', 'posts'));
	
	$db->sql_query(sprintf($sql, 'parent_id', 'forums'));
	$db->sql_query(sprintf($sql, 'position', 'forums'));
	$db->sql_query(sprintf($sql, 'is_forum', 'forums'));

	$db->sql_query(sprintf($sql, 'u_id', 'data'));
	
	echo 'done.</p>';

	$version = '0.9.3';
}

function update_conf_file()
{
	echo '<p>Updating configuration file...';
	
	global $database, $version_new;
	
	$path = 'include/config.inc.php';

$conf_file = <<<CONF
<?php

/*
        This configuration file was automatically generated by aterr-$version_new.
	If you wish to make changes, it is recommended that you keep a backup of this file.
*/

\$version = '$version_new';

\$database = array(
	'host' =>       '$database[host]',
	'user' =>       '$database[user]',
	'password' =>   '$database[password]',
	'database' =>   '$database[database]'
);

?>
CONF;

	if (is_writable($path))
	{
		$fp2 = fopen($path, 'w');

		fwrite($fp2, $conf_file, strlen($conf_file));

		fclose($fp2);
	}
	else
	{
		echo ' configuration file is not writable. Please make include/config.inc.php writable, then re-run this script.</p>';
		die();
	}

	echo ' done.</p>';
}

function update_conf_table()
{
	global $db, $version_new;

	$sql = array(
		"REPLACE INTO config VALUES ('vc_current', '$version_new')",
		"REPLACE INTO config VALUES ('vc_last', " . time() . ")"
	);
	
	foreach ($sql as $query)
	{
		$db->sql_query($query);
	}
}	

function sync_all_forums()
{
	echo '<p>Synchronising forums...';
	
	global $db;

	$forums = new forums;

	$sql = 'SELECT f.id
		FROM forums f
		WHERE f.parent_id = 0';
	
	$db->sql_query($sql);

	$db->sql_data($data);

	foreach ($data as $forum)
	{
		$forums->resync_forum_post_count($forum['id']);
	}

	$forums->resync_forum_positions();

	echo ' done.</p>';
}

?>
